import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: syyyy
 * Date: 2025-09-20
 * Time: 16:05
 */
public class Test {
    static class Node{
        public int val;
        public Node next;
        public Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
    public int singleNumber(int[] nums){
        HashSet<Integer> hashSet=new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if(hashSet.contains(nums[i])){
                hashSet.remove(nums[i]);
            }else {
                hashSet.add(nums[i]);
            }
        }

        for (int i = 0; i < nums.length; i++) {
            if(hashSet.contains(nums[i])){
                return nums[i];
            }
        }
        return -1;
    }

    public Node copyRandomList(Node head){
        HashMap<Node,Node> hashMap=new HashMap<>();
        Node cur=head;
        while (cur != null){
            Node node=new Node(cur.val);
            hashMap.put(cur,node);
            cur=cur.next;
        }
        cur=head;
        while(cur != null){
            hashMap.get(cur).next=hashMap.get(cur.next);
            hashMap.get(cur).random=hashMap.get(cur.random);
            cur=cur.next;
        }

        return hashMap.get(head);
    }

    public int numJewelsInStones(String jewels, String stones) {
        int count=0;
        HashSet<Character> hashSet=new HashSet<>();
        for (int i = 0; i < jewels.length(); i++) {
            char ch=jewels.charAt(i);
            hashSet.add(ch);
        }
        for (int i = 0; i < stones.length(); i++) {
            char ch=stones.charAt(i);
            if(hashSet.contains(ch)){
                count++;
            }
        }
        return count;
    }

    public static void func(String s1,String s2){
        HashSet<Character> hashSet=new HashSet<>();
        String str1 = s1.toUpperCase();
        String str2 = s2.toUpperCase();
        for (int i = 0; i < str2.length(); i++) {
            char ch=str2.charAt(i);
            hashSet.add(ch);
        }

        HashSet<Character> brokenKey =new HashSet<>();

        for (int i = 0; i < str1.length(); i++) {
            char ch=str1.charAt(i);
            if(!hashSet.contains(ch) && !brokenKey.contains(ch)){
                brokenKey.add(ch);
                System.out.println("此按键坏了："+ ch);//这么写是有错误的，坏掉的按钮会被反复输出(在没有brokenKey的情况下）
            }
        }
    }

    public List<String> topKFrequent(String[] words, int k){
        HashMap<String,Integer> hashMap=new HashMap<>();
        for (int i = 0; i < words.length; i++) {
            if(hashMap.get(words[i])==null){
                hashMap.put(words[i],1);
            }else {
                int val=hashMap.get(words[i]);
                hashMap.put(words[i],val++);
            }
        }

        PriorityQueue<Map.Entry<String,Integer>> minHeap=new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if (o1.getValue().compareTo(o2.getValue()) == 0) {
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });

        for(Map.Entry<String,Integer> entry : hashMap.entrySet()) {
            if (minHeap.size() < k) {
                minHeap.offer(entry);
            }else {
                Map.Entry<String,Integer> peek=minHeap.peek();
                if(peek.getValue() < entry.getValue()) {
                    minHeap.poll();
                    minHeap.offer(entry);
                } else if (peek.getValue().compareTo(entry.getValue())==0) {
                    if(peek.getKey().compareTo(entry.getKey()) > 0) {
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }
        ArrayList<String> arrayList=new ArrayList<>();
        for (int i = 0; i < k; i++) {
            String tmp=minHeap.poll().getKey();
            arrayList.add(tmp);
        }
        //到这里它的value都是从小到大的
        Collections.reverse(arrayList);
        return arrayList;
}

    public static void main(String[] args) {

    }
}
